Mark duplicated mails as read even if event creation is skipped.

Akinori MUSHA 10 years ago
parent
commit
d101d60211
2 changed files with 33 additions and 24 deletions
  1. 24 24
      app/models/agents/imap_folder_agent.rb
  2. 9 0
      spec/models/agents/imap_folder_agent_spec.rb

+ 24 - 24
app/models/agents/imap_folder_agent.rb

@@ -228,8 +228,6 @@ module Agents
228 228
 
229 229
         next if (uids = seen[mail.uidvalidity]) && uids.include?(mail.uid)
230 230
 
231
-        next if notified.include?(mail.message_id)
232
-
233 231
         body_parts = mail.body_parts(mime_types)
234 232
         matched_part = nil
235 233
         matches = {}
@@ -275,35 +273,37 @@ module Agents
275 273
           end
276 274
         } or next
277 275
 
278
-        matched_part ||= body_parts.first
276
+        unless notified.include?(mail.message_id)
277
+          matched_part ||= body_parts.first
279 278
 
280
-        if matched_part
281
-          mime_type = matched_part.mime_type
282
-          body = matched_part.decoded
283
-        else
284
-          mime_type = 'text/plain'
285
-          body = ''
286
-        end
279
+          if matched_part
280
+            mime_type = matched_part.mime_type
281
+            body = matched_part.decoded
282
+          else
283
+            mime_type = 'text/plain'
284
+            body = ''
285
+          end
287 286
 
288
-        create_event :payload => {
289
-          'folder' => mail.folder,
290
-          'subject' => mail.subject,
291
-          'from' => mail.from_addrs.first,
292
-          'to' => mail.to_addrs,
293
-          'cc' => mail.cc_addrs,
294
-          'date' => (mail.date.iso8601 rescue nil),
295
-          'mime_type' => mime_type,
296
-          'body' => body,
297
-          'matches' => matches,
298
-          'has_attachment' => mail.has_attachment?,
299
-        }
287
+          create_event :payload => {
288
+            'folder' => mail.folder,
289
+            'subject' => mail.subject,
290
+            'from' => mail.from_addrs.first,
291
+            'to' => mail.to_addrs,
292
+            'cc' => mail.cc_addrs,
293
+            'date' => (mail.date.iso8601 rescue nil),
294
+            'mime_type' => mime_type,
295
+            'body' => body,
296
+            'matches' => matches,
297
+            'has_attachment' => mail.has_attachment?,
298
+          }
299
+
300
+          notified << mail.message_id if mail.message_id
301
+        end
300 302
 
301 303
         if options['mark_as_read']
302 304
           log 'Marking as read'
303 305
           mail.mark_as_read
304 306
         end
305
-
306
-        notified << mail.message_id if mail.message_id
307 307
       }
308 308
 
309 309
       notified.slice!(0...-IDCACHE_SIZE) if notified.size > IDCACHE_SIZE

+ 9 - 0
spec/models/agents/imap_folder_agent_spec.rb

@@ -228,6 +228,15 @@ describe Agents::ImapFolderAgent do
228 228
         }
229 229
         lambda { @checker.check }.should change { Event.count }.by(2)
230 230
       end
231
+
232
+      it 'should create just one event for multiple mails with the same Message-Id' do
233
+        @mails.first.message_id = @mails.last.message_id
234
+        @checker.options['mark_as_read'] = true
235
+        @mails.each { |mail|
236
+          stub(mail).mark_as_read.once
237
+        }
238
+        lambda { @checker.check }.should change { Event.count }.by(1)
239
+      end
231 240
     end
232 241
   end
233 242
 end